#!/usr/bin/env python3
"""
环境配置脚本
用于设置CAMEL多智能体旅游导航系统的环境变量
"""

import os
import sys
from pathlib import Path

def create_env_file():
    """创建.env文件"""
    env_content = """# OpenAI API配置 (必需)
OPENAI_API_KEY=your_openai_api_key_here

# Supabase 配置 (必需)
SUPABASE_URL=your_supabase_url
SUPABASE_ANON_KEY=your_supabase_anon_key
SUPABASE_SERVICE_ROLE_KEY=your_service_role_key
SUPABASE_DB_URL=postgresql://user:password@host:port/database

# 图片搜索API配置
PEXELS_API_KEY=uMYqeI26ls0QnybeZmhXrDpCvi2OD9pY3igAAFJXbY8Y4d5RUAfiuOZX
UNSPLASH_ACCESS_KEY=your_unsplash_key_optional

# 云存储配置 (可选)
CLOUD_STORAGE_TYPE=local
AWS_ACCESS_KEY_ID=your_aws_key
AWS_SECRET_ACCESS_KEY=your_aws_secret
AWS_S3_BUCKET=your_bucket_name

# 本地存储路径
LOCAL_STORAGE_PATH=/Users/a1/work/lukaliou123OrientDirector/OrientDirector/media_storage

# 地图API配置 (可选)
GOOGLE_MAPS_API_KEY=your_google_maps_key
AMAP_API_KEY=your_amap_key

# 视频API配置 (可选)
YOUTUBE_API_KEY=your_youtube_key
VIMEO_ACCESS_TOKEN=your_vimeo_token

# 环境配置
isUsedomainnameaddress=false
"""
    
    env_file = Path(".env")
    
    if env_file.exists():
        print("⚠️  .env文件已存在")
        response = input("是否覆盖现有的.env文件？(y/N): ")
        if response.lower() != 'y':
            print("❌ 取消创建.env文件")
            return False
    
    try:
        with open(env_file, 'w', encoding='utf-8') as f:
            f.write(env_content)
        print("✅ .env文件创建成功")
        return True
    except Exception as e:
        print(f"❌ 创建.env文件失败: {e}")
        return False

def create_media_storage_dir():
    """创建媒体存储目录"""
    media_dir = Path("media_storage")
    try:
        media_dir.mkdir(exist_ok=True)
        print("✅ 媒体存储目录创建成功")
        return True
    except Exception as e:
        print(f"❌ 创建媒体存储目录失败: {e}")
        return False

def check_dependencies():
    """检查依赖包"""
    required_packages = [
        'fastapi',
        'uvicorn',
        'openai',
        'python-dotenv',
        'aiohttp',
        'pillow',
        'supabase'
    ]
    
    missing_packages = []
    
    for package in required_packages:
        try:
            __import__(package.replace('-', '_'))
        except ImportError:
            missing_packages.append(package)
    
    if missing_packages:
        print("❌ 缺少以下依赖包:")
        for package in missing_packages:
            print(f"   - {package}")
        print("\n请运行以下命令安装依赖:")
        print("pip install -r requirements.txt")
        return False
    else:
        print("✅ 所有依赖包已安装")
        return True

def main():
    """主函数"""
    print("🚀 CAMEL多智能体旅游导航系统 - 环境配置")
    print("=" * 50)
    
    # 检查依赖
    print("\n1. 检查依赖包...")
    deps_ok = check_dependencies()
    
    # 创建.env文件
    print("\n2. 创建环境配置文件...")
    env_ok = create_env_file()
    
    # 创建媒体存储目录
    print("\n3. 创建媒体存储目录...")
    media_ok = create_media_storage_dir()
    
    print("\n" + "=" * 50)
    
    if env_ok and media_ok:
        print("✅ 环境配置完成！")
        print("\n📝 下一步:")
        print("1. 编辑.env文件，填入您的API密钥")
        print("2. 运行 python start_app.py 启动系统")
        
        if not deps_ok:
            print("3. 安装缺少的依赖包")
    else:
        print("❌ 环境配置失败，请检查错误信息")
        sys.exit(1)

if __name__ == "__main__":
    main()
